#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging
import datetime
import struct
import binascii
import time
import msgpack

#
DAEMON                     = 0x10
DAEMON_STATUS              = 0x11
#
STORAGE                    = 0x20
#STORAGE_STATUS      = 0x21 # online/offline
#STORAGE_AVAILABLE   = 0x22 # disk available
#STORAGE_FRAMELIST   = 0x23 # framelist
STORAGE_MOVIE_FRAME_ALL    = 0x30 # get movie all framelist
STORAGE_MOVIE_FRAME_FIRST  = 0x31 # get movie first framelist
STORAGE_MOVIE_FRAME_LAST   = 0x32 # get movie last framelist
STORAGE_MOVIE_FRAME_RANGE  = 0x33 # get movie range frame
STORAGE_AUDIO_FRAME_ALL    = 0x34 # get audio all framelist
STORAGE_AUDIO_FRAME_FIRST  = 0x35 # get audio first framelist
STORAGE_AUDIO_FRAME_LAST   = 0x36 # get audio last framelist
STORAGE_AUDIO_FRAME_RANGE  = 0x37 # get audio range frame
STORAGE_MOVIE_GETFRAME     = 0x40 # get movie frame object
STORAGE_AUDIO_GETFRAME     = 0x41 # get audio frame object

#
CAMERA                     = 0x40
CAMERA_STATUS              = 0x41
CAMERA_LIST                = 0x42
CAMERA_IPADDRESS           = 0x43
CAMERA_LOGS                = 0x44
CAMERA_SNAPSHOT            = 0x45
CAMERA_MOVIE_CAPTURE_START = 0x46
CAMERA_MOVIE_CAPTURE_STOP  = 0x47
CAMERA_AUDIO_CAPTURE_START = 0x48
CAMERA_AUDIO_CAPTURE_STOP  = 0x49

class FrameBuilder(object):
    '''
        通信フレームの組み立てクラス
    '''

    def __init__(self):
        self.request_id = 1
        self._logger = logging.getLogger('camfwlib.fb')

    def send_request(self, sock, Type, Param=None):
        '''
            [camfwlib] リクエスト送信
        '''
        request = {}
        request['header'] = 'camfw req'
        request['time']   = datetime.datetime.today().strftime("%Y/%m/%d %H:%M:%S")
        request['id']     = self.request_id
        request['type']   = Type
        if Param:
            request['param']  = Param
            request['length'] = len(Param)
        else:
            request['length'] = 0

        pack_request = msgpack.packb(request)
        sock.sendall(pack_request)

    def recv_response(self, sock):
        '''
            [camfwlib] レスポンス受信
        '''
        unpacker = msgpack.Unpacker()
        sock.setblocking(1)
        while True:
            buf = sock.recv(4096)
            if not buf:
                break
            unpacker.feed(buf)
            for value in unpacker:
                 return value
        return None

    def recv_request(self, sock):
        '''
            [camfwd] リクエスト受信
        '''
        unpacker = msgpack.Unpacker()
        sock.setblocking(1)
        while True:
            buf = sock.recv(4096)
            if not buf:
                break
            unpacker.feed(buf)
            for value in unpacker:
                 return value
        return None

    def send_response(self, sock, Type, Param=None):
        '''
            [camfwd] レスポンス送信
        '''
        response = {}
        response['header'] = 'camfw req'
        response['time']   = datetime.datetime.today().strftime("%Y/%m/%d %H:%M:%S")
        response['id']     = self.request_id
        response['type']   = Type
        if Param:
            response['param']  = Param
            response['length'] = len(Param)
        else:
            response['length'] = 0

        pack_response = msgpack.packb(response)
        sock.sendall(pack_response)

